#pragma once
#include <iostream>
#include <fstream>
#include <string>
#include <cstdio>
#include <string>
#include <ctime>
#include <unistd.h>
#include <cstdarg>
#include <unistd.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include "LockGuard.hpp"

int openDev(const std::string &dev,int flag)
{
    int fd=open(dev.c_str(),flag);
    if(fd<0)
    {
        std::cerr<<"open error"<<std::endl;
        return -1;
    }
    return fd;
}
void Setserver(struct sockaddr_in &server,uint16_t &serverport)
{
    bzero(&server, sizeof(server));
    server.sin_port = htons(serverport);
    server.sin_addr.s_addr = INADDR_ANY;
    server.sin_family=AF_INET;
}


class InetAddr
{
    void GetAddress(uint16_t *Port, std::string *Ip)
    {
        *Port = ntohs(_Addr.sin_port);
        *Ip = inet_ntoa(_Addr.sin_addr);
    }

public:
    InetAddr(struct sockaddr_in &Addr)
        : _Addr(Addr)
    {
        GetAddress(&_Port, &_Ip);
    }
    InetAddr(const std::string &ip,uint16_t port)
         :_Ip(ip),_Port(port)
    {
        _addr.sin_family = AF_INET;
        _addr.sin_port = htons(_port);
        _addr.sin_addr.s_addr = inet_addr(_ip.c_str());
    }
    InetAddr()
    {}
    bool operator==(const InetAddr&addr)
    {
        if(_Ip==addr._Ip&&_Port==addr._Port)
        {
            return true;
        }
        return false;
    }
    uint16_t Port()
    {
        return _Port;
    }
    std::string Ip()
    {
        return _Ip;
    }
    struct sockaddr_in Addr()
    {
        return _Addr;
    }
private:
    struct sockaddr_in _Addr;
    uint16_t _Port;
    std::string _Ip;
};
